// 导入db模块
const db = require('../common/db')

class Comment {
    //新增评论
    comment_add(user_url, product_url, rating, comment, skin_type, name) {
        return new Promise((resolve, reject) => {
            // 执行插入的 SQL 语句    

            db.query(`insert into reviews (user_url, product_url, rating, comment, skin_type, name) 
            VALUES ('${user_url}', '${product_url}', '${rating}', '${comment}','${skin_type}','${name}')`)
                .then(content => {
                    resolve({
                        code: 200,
                        msg: "新增评论成功",
                        status: content.changedRows == 0 ? '正常' : '异常'
                    });
                })
                .catch(error => {
                    reject({
                        code: -1,
                        msg: "新增评论失败",
                        error
                    });
                });
        })
    }

    //删除评论
    comment_remove(id) {
        return new Promise((resolve, reject) => {
            // 根据text字段查询是否存在重复的名称
            db.query(`select id from reviews where id = '${id}'`)
                .then(
                    row => {
                        if (row.length == 0) {
                            resolve({
                                code: 404,
                                msg: `这个ID“${id}”评论不存在`
                            })
                        }
                        else {
                            // 执行删除操作
                            db.query(`delete from reviews where id = '${id}'`)
                                .then(
                                    content => {
                                        resolve({
                                            code: 200,
                                            msg: `删除该ID“${id}”评论成功`,
                                            status: content.changedRows === 0 ? '正常' : '异常'
                                        })
                                    }
                                )
                                .catch(
                                    error => {
                                        reject({
                                            code: -1,
                                            msg: `删除该ID“${id}”评论失败`,
                                            error
                                        })
                                    }
                                )
                        }
                    }
                )

        })
    }

    //获取评论列表
    comment_list(page_num, page_size) {
        return new Promise((resolve, reject) => {
            db.query('select count(*) as total from reviews')
                .then(
                    data => {
                        return data;
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "统计失败",
                            err
                        });
                    }
                )
                .then(
                    data => {
                        let num = (parseInt(page_num) - 1) * parseInt(page_size); // 从哪个位置开始获取
                        let size = parseInt(page_size); // 获取多少条记录
                        // 获取评论数据
                        return db.query(`
                            select * 
                            from reviews 
                            limit ${num}, ${size}
                        `)
                            .then(
                                rows => {
                                    resolve({
                                        code: 200,
                                        msg: "获取评论列表成功",
                                        total: data[0]?.total || 0,
                                        result: rows
                                    });
                                },
                                err => {
                                    reject({
                                        code: -1,
                                        msg: "获取评论列表失败",
                                        err
                                    });
                                }
                            );
                    }
                )
                .catch(error => {
                    reject({
                        code: -1,
                        msg: "查询失败",
                        error
                    });
                });
        });
    }


}


// 导出面试题
module.exports = Comment;